﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace Ha666.Redis.Test
{
    public partial class Form1 : Form
    {

        private Trade trade = null;

        public Form1()
        {
            InitializeComponent();
            trade = new Trade();
            trade.Tid = 78943561326512345;
            trade.Title = "村有地在有有厅地";
            trade.Status = "TRADE_FINISHED";
            trade.Created = new DateTime(2014, 05, 31, 01, 01, 01);
            trade.Modified = new DateTime(2015, 05, 01, 00, 00, 00);
            trade.SellerNick = "要肝在十月份地的";
            trade.BuyerNick = "tbha666";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringKey key = new StringKey("ha666");
            List<string> list = key.Time(0);
            lblTime.Text = string.Format("{0}.{1}", GetTime(list[0]).ToString("yyyy-MM-dd HH:mm:ss"), list[1]);
        }

        public DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                ProtobufKey key = new ProtobufKey("ha666.trade." + trade.Tid);
                trade.Modified = DateTime.Now;
                key.Set(0, trade);
                Trade trade1 = key.Get<Trade>(0);
                MessageBox.Show(trade1.Modified.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            catch (Exception exception)
            {
                MessageBox.Show(string.Format("出错了：{0}", exception.Message));
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ProtobufMapSet key = new ProtobufMapSet("ha666_map");
            key.HSet(0, "trade", trade);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ProtobufMapSet key = new ProtobufMapSet("ha666_map");
            Trade trade = key.Hget<Trade>(0, "trade");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ProtobufMapSet key = new ProtobufMapSet("ha666_map");
            key.HSet(0, "currtime", DateTime.Now.ToString("HH:mm:ss.fff"));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ProtobufMapSet key = new ProtobufMapSet("ha666_map");
            string time = key.Hget<string>(0, "currtime");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            list.Add("a");
            list.Add("b");
            list.Add("c");
            list.Add("d");
            list.Add("e");
            list.Add("f");
            StringSet key = new StringSet("ha666_set");
            key.Sadds(0, list);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Stopwatch time = new Stopwatch();
            time.Start();
            for (int i = 0; i < 100000; i++)
            {
                StringSet key = new StringSet("ha666_set");
                List<string> list = key.Smember<string>(0);
            }
            time.Stop();
            MessageBox.Show("获取100000次，耗时：" + time.ElapsedMilliseconds + "毫秒！");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Stopwatch time = new Stopwatch();
            time.Start();
            StringKey key = new StringKey("ha666_set_ex");
            key.Set(0, "1234567890", 600, null, null);
            time.Stop();
            MessageBox.Show("耗时：" + time.ElapsedMilliseconds + "毫秒！");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Stopwatch time = new Stopwatch();
            time.Start();
            StringKey key = new StringKey("ha666_set_ex");
            var obj = key.Get<string>(0);
            var ttl = key.TTL(0);
            time.Stop();
            MessageBox.Show("耗时：" + time.ElapsedMilliseconds + "毫秒！");
        }

    }
}
